/*
 * Created by JFormDesigner on Thu Jul 01 09:14:09 CST 2021
 */

package com.minelx.statementplus.ui.form.ui.memberbased;

import com.minelx.sqlstructures.statements.clause.Sql;
import com.minelx.statementplus.ui.form.dao.BaseListModel;
import com.minelx.statementplus.ui.form.dao.IChildSqlBuilderListModel;
import com.minelx.statementplus.ui.form.dao.TableAndFieldValuesListModel;
import com.minelx.statementplus.ui.form.service.tablemapping.ITableMapping;
import com.minelx.statementplus.ui.form.swing.OnClosingDialog;
import com.minelx.statementplus.ui.form.ui.memberbased.addingchildsql.AddingChildSqlDialog;
import com.minelx.statementplus.ui.form.ui.memberbased.addingfield.AddingFieldDialog;
import com.minelx.statementplus.ui.form.vo.ChildSqlBuilderPairValue;
import com.minelx.statementsplus.CombinedSql;
import com.minelx.statementsplus.IChildSqlBuilder;
import com.minelx.statementsplus.MajorSql;
import com.minelx.woods.task.core.FieldCollector;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.function.Consumer;

import static java.util.stream.Collectors.toList;

public class ChildSqlBuildingDialog extends OnClosingDialog {

	private final MajorSql majorSqlBuilder;

	private final ITableMapping tableMapping;

	private final BaseListModel<ChildSqlBuilderPairValue> childSqlListModel;

	private final TableAndFieldValuesListModel tableAndFieldValuesListModel;

	public ChildSqlBuildingDialog(Window owner,
								  MajorSql majorSqlBuilder,
								  ITableMapping tableMapping,
								  Consumer<List<String>> combinedSqlBuilderConsumer) {
		super(owner, e -> {});

		initComponents();
		this.tableMapping = tableMapping;
		this.majorSqlBuilder = majorSqlBuilder;
		tableAndFieldValuesListModel = new TableAndFieldValuesListModel();
		childSqlListModel = new IChildSqlBuilderListModel();
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				combinedSqlBuilderConsumer.accept(combineToChildSqlList());
			}
		});

		// work for combine
	}

	void generateButtonActionPerformed(ActionEvent e) {
		CombinedSql combined = combine();
		resultText.setText("major = " + combined.major().head().content() + "\n" + "child = " + String.join("\n", combined.children()));
	}

	void generateJSButtonActionPerformed(ActionEvent e) {
		CombinedSql combined = combine();
		resultText.setText("document.querySelector(\"#sql_main\").value = '" + combined.major().head().content() + "';" +
				"document.querySelector(\"#sql_sub\").value = '" + String.join("\n", combined.children()).replace("\n", "\\n") + "';");
	}

	void addSqlButtonActionPerformed(ActionEvent e) {
		new AddingChildSqlDialog(this, childSqlListModel, tableMapping).setVisible(true);
	}

	void setQueryFieldsButtonActionPerformed(ActionEvent e) {
		new AddingFieldDialog(this, tableAndFieldValuesListModel, tableMapping).setVisible(true);
	}

	private CombinedSql combine() {
		List<String> additionalSqlBuilders = combineToChildSqlList();
		return new CombinedSql(majorSqlBuilder, additionalSqlBuilders);
	}

	private List<String> combineToChildSqlList() {
		List<String> additionalSqlBuilders = childSqlListModel.toList().stream()
				.map(ChildSqlBuilderPairValue::childSql)
				.collect(toList());
		FieldCollector.collect(tableAndFieldValuesListModel.toList()).stream()
				.map(IChildSqlBuilder::asSql)
				.map(Sql::toText)
				.forEach(additionalSqlBuilders::add);
		return additionalSqlBuilders;
	}

	private void copyToClipBoardButtonActionPerformed(ActionEvent e) {
		Toolkit.getDefaultToolkit().getSystemClipboard()
				.setContents(new StringSelection(resultText.getText()), null);
	}

	void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		label2 = new JLabel();
		generateButton = new JButton();
		generateJSButton = new JButton();
		scrollPane3 = new JScrollPane();
		resultText = new JTextArea();
		label3 = new JLabel();
		addSqlButton = new JButton();
		copyToClipBoardButton = new JButton();
		setQueryFieldsButton = new JButton();
		label4 = new JLabel();

		//======== this ========
		setTitle("\u5546\u6237\u53f7\u9a71\u52a8\u67e5\u8be2");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		var contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- label2 ----
		label2.setText("\u9700\u8981\u67e5\u8be2\u5546\u6237\u5bf9\u5e94\u7684\u54ea\u4e9b\u4fe1\u606f?");
		label2.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 26));
		contentPane.add(label2);
		label2.setBounds(50, 80, 385, 60);

		//---- generateButton ----
		generateButton.setText("\u751f\u6210");
		generateButton.setFont(new Font("Microsoft JhengHei Light", Font.BOLD, 22));
		generateButton.addActionListener(e -> generateButtonActionPerformed(e));
		contentPane.add(generateButton);
		generateButton.setBounds(180, 555, 99, 43);

		//---- generateJSButton ----
		generateJSButton.setText("\u751f\u6210JS");
		generateJSButton.setFont(new Font("Microsoft JhengHei Light", Font.BOLD, 22));
		generateJSButton.addActionListener(e -> generateJSButtonActionPerformed(e));
		contentPane.add(generateJSButton);
		generateJSButton.setBounds(45, 555, 99, 43);

		//======== scrollPane3 ========
		{

			//---- resultText ----
			resultText.setLineWrap(true);
			resultText.setEditable(false);
			scrollPane3.setViewportView(resultText);
		}
		contentPane.add(scrollPane3);
		scrollPane3.setBounds(530, 95, 530, 495);

		//---- label3 ----
		label3.setText("\u7ed3\u679c");
		label3.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 26));
		contentPane.add(label3);
		label3.setBounds(530, 38, 385, 60);

		//---- addSqlButton ----
		addSqlButton.setText("\u6dfb\u52a0\u81ea\u5b9a\u4e49\u67e5\u8be2");
		addSqlButton.setFont(new Font("Microsoft JhengHei Light", Font.BOLD, 22));
		addSqlButton.addActionListener(e -> addSqlButtonActionPerformed(e));
		contentPane.add(addSqlButton);
		addSqlButton.setBounds(250, 170, 195, 38);

		//---- copyToClipBoardButton ----
		copyToClipBoardButton.setText("\u590d\u5236\u7ed3\u679c");
		copyToClipBoardButton.setFont(new Font("Microsoft JhengHei Light", Font.BOLD, 22));
		copyToClipBoardButton.addActionListener(e -> copyToClipBoardButtonActionPerformed(e));
		contentPane.add(copyToClipBoardButton);
		copyToClipBoardButton.setBounds(910, 610, 149, 43);

		//---- setQueryFieldsButton ----
		setQueryFieldsButton.setText("\u9009\u62e9\u5b57\u6bb5");
		setQueryFieldsButton.setFont(new Font("Microsoft JhengHei Light", Font.BOLD, 22));
		setQueryFieldsButton.addActionListener(e -> setQueryFieldsButtonActionPerformed(e));
		contentPane.add(setQueryFieldsButton);
		setQueryFieldsButton.setBounds(50, 170, 173, 38);

		//---- label4 ----
		label4.setText("\u9884\u89c8");
		label4.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 26));
		contentPane.add(label4);
		label4.setBounds(50, 480, 385, 60);

		contentPane.setPreferredSize(new Dimension(1110, 720));
		setSize(1110, 720);
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JLabel label2;
	private JButton generateButton;
	private JButton generateJSButton;
	private JScrollPane scrollPane3;
	private JTextArea resultText;
	private JLabel label3;
	private JButton addSqlButton;
	private JButton copyToClipBoardButton;
	private JButton setQueryFieldsButton;
	private JLabel label4;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}